package vsvm.gui.menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

import vsvm.classifier.AbstractClassifier;
import vsvm.gui.Environment;
import vsvm.gui.IActionsProvider;
import vsvm.gui.forms.ClassifierProgressWindow;
import vsvm.gui.forms.TrainClassifierForm;
//import vsvm.gui.forms.ConfigureSvmLibDialog;
import vsvm.gui.forms.TestClassifierForm;

public class ClassifierMenu implements ActionListener {
	static String AC_CONFIGURE = "configure";
	static String AC_TRAIN = "train";
	static String AC_TEST = "test";
	static String AC_CLASSIFY = "classify";
	static String AC_DELETE = "delete";
	
	private AbstractClassifier cls;
	
	public ClassifierMenu(AbstractClassifier c) {
		cls = c;		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(AC_CONFIGURE)) {
			cls.configure();
		} 
		else if (e.getActionCommand().equals(AC_CONFIGURE)) {
			
		} 
		else if (e.getActionCommand().equals(AC_TRAIN)) {
			TrainClassifierForm f = new TrainClassifierForm(Environment.getEnvironment().getMainFrame(), cls);
			f.setVisible(true);
			//ClassifierProgressWindow w = new ClassifierProgressWindow(null);
			//Environment.getEnvironment().getMainFrame().addWindow(w);			
		} 
		else if (e.getActionCommand().equals(AC_TEST)) {
			TestClassifierForm f = new TestClassifierForm(Environment.getEnvironment().getMainFrame(), cls);
			f.setVisible(true);			
		} 
		else if (e.getActionCommand().equals(AC_CLASSIFY)) {
			
		} 
		else if (e.getActionCommand().equals(AC_DELETE)) {
			if (JOptionPane.showConfirmDialog(Environment.getEnvironment().getMainFrame(),
					"Do you really want to delete this classifier?") == JOptionPane.YES_OPTION)
				Environment.getEnvironment().getWorkspace().deleteClassifier(cls);
			
		} else {
			cls.executeAction(e.getActionCommand());
			
		}
	}
	
	
	public static JPopupMenu getPopup(AbstractClassifier c) {
		JPopupMenu menu = new JPopupMenu();
		ArrayList<JComponent> items = createItems(c);
		for (Iterator<JComponent> iter = items.iterator(); iter.hasNext();) {
			menu.add(iter.next());			
		}
		
		return menu;	
	}
	
	private static ArrayList<JComponent> createItems(AbstractClassifier c) {
		ArrayList<JComponent> arr = new ArrayList<JComponent>();
		ClassifierMenu dsm = new ClassifierMenu(c);
		
		JMenuItem miCreate = new JMenuItem("Configure");
		miCreate.setActionCommand(AC_CONFIGURE);
		miCreate.addActionListener(dsm);
		arr.add(miCreate);
		
		arr.add(new JSeparator());
		
		JMenuItem miTrain = new JMenuItem("Train");
		miTrain.setActionCommand(AC_TRAIN);
		miTrain.addActionListener(dsm);
		arr.add(miTrain);
		
		JMenuItem miTest = new JMenuItem("Test");
		miTest.setActionCommand(AC_TEST);
		miTest.addActionListener(dsm);
		arr.add(miTest);
				
		JMenuItem miClassify = new JMenuItem("Classify");
		miClassify.setActionCommand(AC_CLASSIFY);
		miClassify.addActionListener(dsm);
		//arr.add(miClassify);
		
		arr.add(new JSeparator());
		
		if (c instanceof IActionsProvider) {
			IActionsProvider ap = (IActionsProvider)c;
			String[] act = ap.getActions();
			
			for (int i = 0; i < act.length; i++) {
				JMenuItem mi = new JMenuItem(act[i]);
				mi.setActionCommand(act[i]);
				mi.addActionListener(dsm);
				arr.add(mi);
			}
			
			if (act.length > 0)
				arr.add(new JSeparator());
		}
		
		JMenuItem miDelete = new JMenuItem("Delete");
		miDelete.setActionCommand(AC_DELETE);
		miDelete.addActionListener(dsm);
		arr.add(miDelete);
		
		return arr;
	}
	

}
